#include "drop_selector.h"
#include "ui_drop_selector.h"
#include "easylogging++.h"
#include <QVBoxLayout>
#include <QGraphicsDropShadowEffect>
#include <QPainter>
#include <QFile>
#include <QPainterPath>
#include <QVBoxLayout>

const int SHADOW_WIDTH = 6;    // 窗口阴影宽度
const int TRIANGLE_WIDTH = 15;  // 小三角形的宽度
const int TRIANGLE_HEIGHT = 10; // 小三角形的高度
const int BORDER_RADIUS = 5;    // 窗口圆角

DropSelector::DropSelector(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::DropSelector),
    m_triangleStart(TRIANGLE_WIDTH),
    m_triangleWidth(TRIANGLE_WIDTH),
    m_triangleHeight(TRIANGLE_HEIGHT)
{
    ui->setupUi(this);
    this->layout()->setContentsMargins(SHADOW_WIDTH,
                                       SHADOW_WIDTH + TRIANGLE_HEIGHT,
                                       SHADOW_WIDTH,
                                       SHADOW_WIDTH);
    setWindowFlags(Qt::Popup | Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);
    setAttribute(Qt::WA_DeleteOnClose);

    auto shadowEffect = new QGraphicsDropShadowEffect(this);
    shadowEffect->setOffset(0, 0);
    shadowEffect->setColor(Qt::red);
    shadowEffect->setBlurRadius(SHADOW_WIDTH);
    this->setGraphicsEffect(shadowEffect);

    /**< 定制样式 */
    QFile styleFile(":/resources/qss/style.qss");
    styleFile.open(QIODevice::ReadOnly);
    QString stylesheet = styleFile.readAll();
    styleFile.close();
    setStyleSheet(styleSheet());

    initEvents();
}

DropSelector::~DropSelector()
{
    LOG(INFO) << "~CurrentStatusSelector";
    delete ui;
}

void DropSelector::setTriangleInfo(int start, int width, int height)
{
    m_triangleStart = start;
    m_triangleWidth = width;
    m_triangleHeight = height;
    this->layout()->setContentsMargins(SHADOW_WIDTH,
                                       SHADOW_WIDTH + TRIANGLE_HEIGHT,
                                       SHADOW_WIDTH,
                                       SHADOW_WIDTH);
}

void DropSelector::setItems(QStringList items)
{
    QVBoxLayout *layout = qobject_cast<QVBoxLayout*>(this->layout());
    // 遍历并删除所有子部件
    QLayoutItem *child;
    while ((child = layout->takeAt(0)) != nullptr) {
        // 注意：如果子部件也是一个布局，你可能需要递归地删除它的子部件
        delete child->widget(); // 删除部件
        delete child; // 删除布局项
    }
    for (int i = 0; i < items.count(); i++)
    {
        QPushButton *button = new QPushButton(items[i]);
        layout->addWidget(button);
    }
    adjustSize();
}

void DropSelector::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QPainter painter(this);
    //setRenderHint(QPainter::RenderHint hint, bool on = true)：用于设置绘图时的渲染提示
    //hint参数表示要设置的渲染提示，比如抗锯齿、文本抗锯齿等。on参数表示是否开启这个渲染提示
    painter.setRenderHint(QPainter::Antialiasing, true);
    painter.setPen(Qt::NoPen); //将画笔设置为无边框
    painter.setBrush(QColor(255, 255, 255)); //设置画刷颜色为灰色

    // 小三角区域，这里是等腰三角形
    //小三角形的三个点的坐标，注意不要忘记窗口边距就行
    QPolygon trianglePolygon;
    trianglePolygon << QPoint(m_triangleStart, m_triangleHeight + SHADOW_WIDTH);
    trianglePolygon << QPoint(m_triangleStart + m_triangleWidth / 2, SHADOW_WIDTH);
    trianglePolygon << QPoint(m_triangleStart + m_triangleWidth, m_triangleHeight + SHADOW_WIDTH);

    QPainterPath drawPath; //用于定义整个绘制路径

    //调用 drawPath.addRoundedRect 方法添加一个带有圆角的矩形路径，用于绘制矩形框的主体部分。
    // 圆角的半径由 BORDER_RADIUS 定义
    //addRoundedRect(const QRectF &rect, qreal xRadius, qreal yRadius)：用于在绘图路径中添加一个带有圆角的矩形
    drawPath.addRoundedRect(QRect(SHADOW_WIDTH, SHADOW_WIDTH + m_triangleHeight,
                                  this->width() - SHADOW_WIDTH * 2, this->height() - SHADOW_WIDTH * 2 - m_triangleHeight),
                            BORDER_RADIUS, BORDER_RADIUS);


    // 矩形路径加上三角形
    //addPolygon(const QPolygonF &polygon)：用于在绘图路径中添加一个多边形
    //polygon参数表示要添加的多边形的顶点坐标
    drawPath.addPolygon(trianglePolygon);

    // 绘制整个路径
    painter.drawPath(drawPath);
}

void DropSelector::initEvents()
{
    connect(ui->unstarted, &QPushButton::clicked, this, &DropSelector::btnClicked);
    connect(ui->onGoing, &QPushButton::clicked, this, &DropSelector::btnClicked);
    connect(ui->done, &QPushButton::clicked, this, &DropSelector::btnClicked);
    connect(ui->closed, &QPushButton::clicked, this, &DropSelector::btnClicked);
}

void DropSelector::btnClicked()
{
    QPushButton *btn = qobject_cast<QPushButton*>(sender());
    if (nullptr == btn)
    {
        return ;
    }

    emit(itemSelected(btn->text()));
}
